import {request} from "../../utils/request";
import config from "../../utils/config";

const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        picUrl: config.picUrl,
        custom: wx.getMenuButtonBoundingClientRect(),
        user: {},
        formData: {album_id: 1},
        url: config.url,
        avatar: '',
        nickname: '',
        phone: '',
        birthday: '',
    },
    onLoad(options) {
        var user = wx.getStorageSync('userInfo')
        if (user) {
            this.setData({user})
        }
        wx.hideShareMenu()
    },
    changeValue(e) {
        var {field, value} = e.currentTarget.dataset
        wx.showModal({
            editable: true,
            placeholderText: value,
            success: res => {
                if (res.cancel) {
                    return false
                }
                this.changeValueBase(field, res.content)
            }
        })
    },
    getAvatar(e) {
        // this.setData({
        //     ['user.avatar']:e.detail.avatarUrl
        // })
        wx.uploadFile({
            url: this.data.url + '/system/api/uploadBase64',
            filePath: e.detail.avatarUrl,
            name: 'file',
            formData: {id: this.data.user.id},
            success: res => {
                res = JSON.parse(res.data)
                if (res.code != 200) {
                    config.toast(res.msg, 201, () => {
                    })
                    return false
                }
                this.setData({
                    ['user.avatar']: res.data,
                    ['user.is_wechat_authentication']:1
                })
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];
                prevPage.setData({
                    userInfo:this.data.user
                })
                wx.setStorageSync('userInfo',this.data.user)
            }
        })
    },
    changename(e) {
        this.changeValueBase('nickname', e.detail.value)
    },
    getPhone(e) {
        request({
            url: '/miniprogram/user/getPhoneNumber',
            data: {code: e.detail.code, id: this.data.user.id},
            type: 'post'
        }).then(res => {
            if (res.code != 200) {
                config.toast(res.msg, 201, () => {
                })
                return
            } else {
                this.setData({
                    [`user.phone`]: res.data,
                    ['user.is_wechat_authentication']:1
                })
                wx.setStorageSync('userInfo',this.data.user)
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];
                prevPage.setData({
                    userInfo:this.data.user
                })
            }
        })
        console.log(e)
    },
    changeValueBase(field, value) {
        request({
            url: "/miniprogram/user/editUserField",
            data: {field, value, user_id: this.data.user.id},
            type: 'post'
        }).then(res => {
            wx.showToast({title: res.msg, icon: 'none'})
            if (res.code != 200) {
                return false
            }
            this.setData({
                [`user.${field}`]: value,
                ['user.is_wechat_authentication']:1
            })
            wx.clearStorageSync()
            wx.setStorageSync('userInfo', this.data.user)
            var pages = getCurrentPages();
            var currPage = pages[pages.length - 1];   //当前页面
            var prevPage = pages[pages.length - 2];  //上一个页面
//直接调用上一个页面的 setData() 方法，把数据存到上一个页面中去
            prevPage.setData({
                userInfo:this.data.user
            })
        })
    },
    changeImg() {
        this.upload(1, res => {
            wx.showToast({title: res.msg})
            if (res.code == 200) {
                this.changeValueBase('avatar', config.url + res.data.src)
            }
        })
    },
    upload(count, callback) {
        wx.showLoading()
        wx.chooseImage({
            count: count,
            sizeType: ['compressed', 'original'],
            success: res => {
                for (let v of res.tempFiles) {
                    wx.uploadFile({
                        url: config.url + '/system/api/uploadImage',
                        filePath: v.path,
                        name: 'file',
                        formData: this.data.formData,
                        success: res => {
                            wx.hideLoading()
                            callback(JSON.parse(res.data))
                        }
                    })
                }
            },
            fail: err => {
                wx.hideLoading()
            }
        })
    },
    changeDate(e) {
        var field = 'birthday', value = e.detail.value
        this.changeValueBase(field, value)
    },
    nav(){
        wx.navigateBack({delta:1})
    }
})